home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++,comp.os.linux.development.apps,comp.os.linux.misc
- Path: grizzly.cs.washington.edu!chapman
- From: chapman@grizzly.cs.washington.edu (Randy Chapman)
- Subject: Re: Are str* functions okay in C++?
- Followup-To: comp.lang.c++,comp.os.linux.development.apps,comp.os.linux.misc
- X-Newsreader: TIN [version 1.2 PL2]
- Sender: news@beaver.cs.washington.edu (USENET News System)
- Organization: Computer Science & Engineering, U of Washington, Seattle
- Message-ID: <Do61wK.IEG@beaver.cs.washington.edu>
- References: <4hpvi0$gt6@news.platinum.com> <Do54G9.4Ly.0.server@indra.com>
- X-Nntp-Posting-Host: grizzly.cs.washington.edu
- Date: Tue, 12 Mar 1996 17:57:56 GMT
-
- Bear Giles (bear@) wrote:
- : In article <4hpvi0$gt6@news.platinum.com>,
- : Mark Juric <dcmark@platinum.com> wrote:
- : > path = new char[strlen(mbuf)+1];
- : > strcpy(path,mbuf);
-
- : One quick note: there's a "strdup()" function which handles this
- : for you, although it uses "free" instead of "delete."
-
- : Also, another useful technique for this type of code is:
-
- : void procedure (argument...)
- : {
- : static char * buffer = 0;
-
- : if (buffer == 0)
- : buffer = malloc (2048); /* or any _large_ number */
-
- : ...
- : sprintf (buffer, format,...)
- : path = strdup (buffer);
- : ...
-
- : The idea is that "buffer" is a captive memory leak -- you allocate a
- : large buffer once and never release it. That increases the runtime
- : size of your program, but eliminates the performance hit and possible
- : memory leaks from repeatedly allocating and deleting small blocks.
-
- However, it limits you to non-multithreaded programs. Its little things
- like this that make trying to use existing code in a multithreaded
- environment really hard sometimes =(
-
- --randy
-
-
-